<template>
  <div>
    <el-card style="margin: 20px">
      <el-row :gutter="20">
        <el-col :span="18">
          <el-form :inline="true" class="demo-form-inline" label-width="80px">
            <el-form-item label="关键字" >
              <el-input placeholder="根据文章标题搜索" v-model="formInline.keyword"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select placeholder="请选择" v-model="formInline.state">
                <el-option v-for="item in statusList" :key="item.label" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="emply">清空</el-button>
              <el-button type="primary" size="small" @click="handelSearch">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="success"
            size="small"
            icon="el-icon-edit"
            @click="handelArticles"
            >新增用户</el-button
          >
        </el-col>
      </el-row>
      <el-alert title="共1条记录" type="info" show-icon> </el-alert>
      <el-table style="width: 100%; margin-top: 20px" :data="articlesData">
        <el-table-column label="序号" width="48px" prop="id" align="center">
        </el-table-column>
        <el-table-column
          label="文章标题"
          align="center"
          width="400px"
        >
         <template #default="scope">
            <span v-html="scope.row.title"></span>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits" align="center">
        </el-table-column>
        <el-table-column label="录入人" prop="username" align="center">
        </el-table-column>
        <el-table-column label="录入时间" prop="createTime" align="center">
        </el-table-column>
        <el-table-column label="状态" prop="state" align="center">
          <template #default="scope">
            <span v-html="scope.row.state ===1 ?'已禁用':'已启用'"></span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240px" align="center">
          <template #default="scope">
            <el-button type="text" @click="perviewDetail(scope.row)">预览 </el-button>
            <el-button type="text" @click="handelUse(scope.row)">{{scope.row.state === 1 ?'启用':'禁用'}}</el-button>
            <el-button type="text" @click="handelEdit(scope.row)" :disabled="scope.row.state === 1">修改</el-button>
            <el-button type="text" @click="handelDelete(scope.row)" :disabled="scope.row.state === 1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <PaginAtion
        @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange"
        :counts="counts"
        :pagesize="params.pagesize"
      ></PaginAtion>
      <!-- 添加文章的 -->
      <el-dialog
        :title="titles"
        :visible.sync="dialogVisible"
        width="50%"
      >
        <el-form label-width="80px" :data="form" :rules="rules">
          <el-form-item label="文章标题" prop="articleBody">
            <el-input placeholder="请输入文章标签" v-model="form.articleBody"></el-input>
          </el-form-item>
          <el-form-item label="文章内容" prop="title">
            <quillEditor
             :options="editorOption"
             v-model="form.title"
          ></quillEditor>
          </el-form-item>
          <el-form-item label="视频地址" prop="videoURL">
            <el-input placeholder="请输入视频地址" v-model="form.videoURL"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 预览文章的 -->
          <el-dialog
        title="预览文章"
        :visible.sync="PerviewdialogVisible"
        width="50%"
      >
        <div class="preview-box">
           <div class="preview-title">{{ form.title }}</div>
        <div class="preview-nav">
          <span>{{ form.createTime }}</span>
          <span>{{ form.username }}</span>
          <span class="el-icon-view"></span>
          <span>{{ form.visits }}</span>
        </div>
        <div class="preview-content" v-html="form.articleBody"></div>
      </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="PerviewdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="PerviewdialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, detail, changeState, remove, update } from '@/api/hmmm/articles'
import PaginAtion from '@/components/PaginAtion/index'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { status } from '@/api/hmmm/constants'
export default {
  components: {
    PaginAtion,
    quillEditor

  },
  data () {
    return {
      params: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      articlesData: [],
      counts: 1,
      dialogVisible: false,
      PerviewdialogVisible: false,
      // 富文本编辑器配置
      editorOption: {
        // placeholder: '请在这里输入',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote', 'code-block'], // 引用，代码块
            ['image'] // 上传图片、上传视频
          ]
        }
      },
      form: {
        articleBody: '',
        title: '',
        videoURL: ''
      },
      formInline: {
        keyword: '',
        status: null
      },
      titles: '',
      statusList: [],
      rules: {
        articleBody: [{ required: true, message: '文章标题不能为空', trigger: 'blur' }],
        title: [{ required: true, message: '文章内容不能为空', trigger: 'blur' }],
        videoURL: [{ required: true, message: '视频地址不能为空', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getList()
    this.statusList = status
  },
  methods: {
    handleSizeChange (val) {
      this.params.pagesize = val
      this.getList()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      console.log(val)
      this.params.page = val
      this.getList()
      console.log(`当前页: ${val}`)
    },
    async getList () {
      const res = await list(this.params)
      console.log(res)
      this.articlesData = res.data.items
      this.counts = res.data.counts
    },
    handelArticles () {
      this.titles = '新增文章'
      this.dialogVisible = true
    },
    // 添加
    async save () {
      if (this.titles === '新增文章') {
        console.log(1)
        const res = await add(this.form)
        console.log(res)
        this.form = {}
        this.dialogVisible = false
        this.getList()
      } else {
        const res = await update(this.form)
        console.log(res)
        this.getList()
      }
    },
    // 预览
    async perviewDetail (id) {
      this.PerviewdialogVisible = true
      const { data } = await detail(id)
      console.log(data)
      this.form = data
    },
    // 启用
    async handelUse (row) {
      try {
        await changeState({
          id: row.id,
          state: row.state === 1 ? 0 : 1
        })
        this.$message.success('操作成功')
        row.state = row.state === 1 ? 0 : 1
      } catch (e) {
        this.$message.error('请求错误!')
      }
    },
    // 删除
    async handelDelete (id) {
      console.log(id)
      await this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      const res = await remove(id)
      console.log(res)
      this.$message({
        type: 'success',
        message: '成功删除用户!'
      })
      this.getList()
    },
    // 修改
    async handelEdit (row) {
      this.titles = '修改文章'
      this.dialogVisible = true
      this.form.articleBody = row.articleBody
      this.form.title = row.title
      this.form.videoURL = row.videoURL
    },
    emply () {
      this.formInline = {}
    },
    // 搜索TODO
    async handelSearch () {
      this.params.keyword = this.formInline.keyword
      this.params.state = this.formInline.state
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep(.ql-container .ql-snow) {
  height:200px
}
.preview-box {
  .preview-title {
    font-size: 25px;
    font-weight: 700;
    margin-bottom: 10px;
  }
  .preview-nav {
    span {
      margin-right: 10px;
    }
  }
  .preview-content {
    margin-top: 10px;
    padding: 10px;
    background-color: #f5f5f5;
    border-top: 1px dashed #ccc;
  }
}
</style>
